package com.ruoyi.web.controller.vantController.loginController;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.AliyunSmsUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.BrandInfo;
import com.ruoyi.system.service.vantservice.loginservice.ApiLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/login")
@Api(value = "客户登录接口", tags = {"客户登录接口"})
@CrossOrigin
public class ApiLoginController extends BaseController {

    @Autowired
    private ApiLoginService loginService;

    /**
     * 账号密码登录
     * @param customerLogin
     * @return
     */
    @ApiOperation(value = "客户登录校验用户名和密码")
    @PostMapping("/loginPassword")
    public AjaxResult loginPassword(@Validated @RequestBody CustomerLogin customerLogin) {
        AjaxResult ajaxResult = AjaxResult.success();
        String token = loginService.checkLoginCustomer(customerLogin);
        if (token.equals("1")){
            return AjaxResult.error(202,"账号被停用");
        }else if(token.equals("2")){
            return AjaxResult.error(203,"账号或密码错误");
        }else{
            ajaxResult.put("token",token);
            return ajaxResult;
        }
    }

    /**
     * 获取手机验证码
     * @param customerLogin
     * @return
     */
    @ApiOperation(value = "获取手机验证码")
    @PostMapping("/getPhoneCode")
    public AjaxResult getPhoneCode(@RequestBody CustomerLogin customerLogin){
        System.out.println("------------------------phone:"+customerLogin.getLoginName());
        AjaxResult ajax = AjaxResult.success();
        // 创建短信验证码类对象
        AliyunSmsUtils smsUtils = new AliyunSmsUtils();
        // 生成随机六位验证码
        smsUtils.setNewcode();
        // 获取验证码
        String code = Integer.toString(smsUtils.getNewcode());
        try {
            //发送短信
            SendSmsResponse response = smsUtils.sendSms(customerLogin.getLoginName(), code);
        } catch (ClientException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ajax.put("code", code);
        return ajax;
    }

    /**
     * 根据手机号注册用户信息
     * @param customerLogin
     * @return
     */
    @ApiOperation(value = "添加用户信息")
    @PostMapping("/addCustomer")
    public AjaxResult addCustomer(@Validated @RequestBody CustomerLogin customerLogin){
        AjaxResult ajaxResult = AjaxResult.success();
        String token = loginService.addCustomer(customerLogin);
        if (token.equals("1")){
            return AjaxResult.error(202,"账号被停用");
        }else if(token.equals("2")){
            return AjaxResult.error(203,"账号或密码错误");
        }else{
            ajaxResult.put("token",token);
            return ajaxResult;
        }
    }

    /**
     * 校验用户信息
     * @param customerLogin
     * @return
     */
    @ApiOperation(value = "校验用户信息")
    @PostMapping("/checkLoginPhone")
    public AjaxResult checkLoginPhone(@Validated @RequestBody CustomerLogin customerLogin){
        AjaxResult ajaxResult = AjaxResult.success();
        String token = loginService.checkLoginPhone(customerLogin);
        if (token.equals("1")){
            return AjaxResult.error(202,"账号被停用");
        }else if(token.equals("2")){
            return AjaxResult.error(203,"不存在账号信息");
        }else{
            ajaxResult.put("token",token);
            return ajaxResult;
        }
    }

    /**
     * 修改密码
     * @param customerLogin
     * @return
     */
    @ApiOperation(value = "修改密码")
    @PostMapping("/updateCustomer")
    public AjaxResult updateCustomer(@Validated @RequestBody CustomerLogin customerLogin){
        return toAjax(loginService.updateCustomer(customerLogin));
    }
}
